package com.sdvdxl.beans;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.sdvdxl.exceptios.NetworkException;
import com.sdvdxl.exceptios.UnGrantException;
import com.sdvdxl.oauth.GrantManager;

/**
 * 用户详细信息<br>
 * 详细信息请查阅本类方法说明
 * @see User
 * @author sdvdxl (sdvdxl@163.com)
 *
 */
public class SuperUser extends User {
	String userdetail;// 自我介绍
	int job;// 职业
	Date birthday;// 出生日期
	int marriage;// 婚姻
	int sex;// 性别
	int blood;// 血型
	int constellation;// 星座
	int trade;// 当前职业
	int figure;// 体型

	/**
	 * 构造登陆用户 创建信息
	 * @throws UnGrantException 
	 * @throws NetworkException 
	 */
	public SuperUser() throws UnGrantException, NetworkException {
		super();
		xmlURL = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo?" 
				+ "access_token=" + GrantManager.ACCESS_TOKEN
				+ "&format=xml";
		getUserInf();
	}

	public int getJob() {
		return job;
	}

	public void setJob(int job) {
		this.job = job;
	}

	public String getUserdetail() {
		return userdetail;
	}

	public void setUserdetail(String userdetail) {
		this.userdetail = userdetail;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int getMarriage() {
		return marriage;
	}

	public void setMarriage(int marriage) {
		this.marriage = marriage;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public int getBlood() {
		return blood;
	}

	public void setBlood(int blood) {
		this.blood = blood;
	}

	public int getConstellation() {
		return constellation;
	}

	public void setConstellation(int constellation) {
		this.constellation = constellation;
	}

	public int getTrade() {
		return trade;
	}

	public void setTrade(int trade) {
		this.trade = trade;
	}

	public int getFigure() {
		return figure;
	}

	public void setFigure(int figure) {
		this.figure = figure;
	}

	/**
	 * 解析xml， 取出用户信息
	 * @throws NetworkException 链接提交url或者解析xml出错
	 * @throws UnGrantException 未授权异常
	 */
	private void getUserInf() throws NetworkException, UnGrantException {
		Document doc = null;
		Node node = null;
		String basic = "//passport_users_getInfo_response/";
		
		try {
			URL url = new URL(xmlURL);
			URLConnection uc = url.openConnection();

			doc = new SAXReader().read(uc.getInputStream());
		} catch (DocumentException | IOException e) {
			throw new NetworkException("网络异常:" + e);
		} 
		
		node = doc.selectSingleNode(basic + "birthday");
		try {
			setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(node.getText().trim()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		node = doc.selectSingleNode(basic + "sex");
		if (node==null) {
			throw new UnGrantException("未授权");
		}
		setSex(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "marriage");
		setMarriage(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "blood");
		setBlood(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "constellation");
		setConstellation(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "figure");
		setFigure(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "trade");
		setTrade(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "job");
		setJob(Integer.parseInt(node.getText().trim()));
		
		node = doc.selectSingleNode(basic + "userdetail");
		if (node==null) {
			setUserdetail("");
		}else {
			setUserdetail(node.getText().trim());
		}
	}
	
	@Override
	public String toString() {
		return super.toString() + " birthday:" + birthday
				+ " marriage:" + marriage + " sex:" + sex
				+ " blood:" + blood + " constellation:" + constellation
				+ " figure:" + figure + " trade:" + trade + " job:" + job;
	}
}
